package edu.spec.student.dao.impl;

import edu.spec.student.dao.iface.IQuestionnaireDao;
import edu.spec.student.model.questionnaire.Questionnaire;
import edu.spec.basic.model.enumeration.Status;
import org.hibernate.Query;
import org.hibernate.Session;
import org.madgecko.basic.dao.BaseDao;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Author       : MadFrog
 * Date         : 2018/2/18 22:22
 * Description  : “问卷调查”数据访问层 实现
 */
@Repository
public class QuestionnaireDao extends BaseDao<Questionnaire> implements IQuestionnaireDao {

    @Override
    public List<Questionnaire> listLatestByStudent(Integer studentId) {
        String hql = "select sq.questionnaire from StudentQuestionnaire sq where sq.student.id=" + studentId + " and sq.questionnaire.closeDate>:currentDate and sq.questionnaire.status=" + Status.ENABLED.ordinal();
        Session s = this.getSession();
        Query query = s.createQuery(hql.toString());
        query.setTimestamp("currentDate", new Date());//注意这里，用setTimestamp﻿e，不要用setDate
        List<Questionnaire> qList = query.list();
        return qList;
    }

    @Override
    public Pager<Questionnaire> findByCon() {
        String hql = "from Questionnaire order by createDate desc";
        return this.find(hql);
    }
}
